<template>
  <!-- 主体区域 -->
  <section id="app">
    <TodoHeader @doTask="addTask"></TodoHeader>
    <TodoMain v-bind:list="list" @deleteTask="delTask"></TodoMain>
    <TodoFooter v-bind:totalCount="list.length" @clear="clear"></TodoFooter>
  </section>
</template>

<script>
import TodoHeader from './components/TodoHeader.vue'
import TodoMain from './components/TodoMain.vue'
import TodoFooter from './components/TodoFooter.vue'

export default {
  data () {
    return {
      list: JSON.parse(localStorage.getItem('list')) || [
        {id: 1, name: '吃东西'},
        {id: 2, name: '看电影'},
        {id: 3, name: '玩游戏'}
      ]
    }
  },
  methods: {
    addTask (value) {
      this.list.unshift(value)
    },
    delTask(value) {
      this.list = this.list.filter(item => item.id !== value)
    },
    clear() {
      this.list = []
    }
  },
  watch: {
    list: {
      deep: true,
      handler (newVal) {
        localStorage.setItem('list', JSON.stringify(newVal))
      }
    }
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter
  }
}
</script>

<style>

</style>
